/**
 * 招聘详情
 * @author  Jello
 * @create  2018/10/23 17:19
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var spinner = new Spinner(opts);
var sessionStorage = window.sessionStorage;
var workId = ""; //招聘id
var userid_sign = "";
var identy = ""; //用户的唯一标识
var status = ""; //招工状态0正在招工 1已经招满
var userName = ""; //是否实名
var teacherTel = "" //咨询手机号码
var reMoney = 0.00; //返费金额
var reTime = ""; //返费时间（单位：月）
var interviewTime = ""; //面试时间
var mechanismId = ""; //企业id
var recruitAddress = ""; //面试地址
var mechanismName = ""; //企业名称

$(function() {
	userid_sign = sessionStorage.getItem("userid_sign");
	upIdentity = sessionStorage.getItem("upIdentity")
	//如果是扫码进来的就取链接上面的，如果是直接登录进来的就取本身cookie里面的identity
	if(upIdentity) {
		identy = upIdentity; //取链接上面的,已经存好的
	} else {
		identy = identity; //取本身
	}

	//轮播图
	var mySwiper = new Swiper('.swiper-container', {
		initialSlide: 0,
		// 轮播图的方向，也可以是vertical方向
		direction: 'horizontal',
		//播放速度
		loop: true,
		observer: true,
		observeParents: true,
		// 自动播放时间
		autoplay: 4500,
		// 播放的速度
		speed: 300,
		// 如果需要分页器，即下面的小圆点
		pagination: '.swiper-pagination',
		// 这样，即使我们滑动之后， 定时器也不会被清除
		autoplayDisableOnInteraction: false,
		paginationClickable: true,
		pagination: '.swiper-pagination',
		paginationType: 'custom',
		paginationCustomRender: function(swiper, current, total) {
			var html = '';
			for(var i = 1; i <= total; i++) {
				if(current == i) {
					html += '<span class="swiper-pagination-customs swiper-pagination-customs-active"></span>';
				} else {
					html += '<span class="swiper-pagination-customs"></span>';
				}
			}
			return html; //返回所有的页码html
		}
	});

	//截取招聘id
	workId = getParam("id");
	//查询招聘详情
	var param = {};
	param.workId = workId;
	queryWorkDetail(param, function(resultObj) {
		var detailObj = resultObj.data;
		//轮播图
		var swiperList = detailObj.imageList.split(";");
		for(var k = 0; k < swiperList.length; k++) {
			var hot = '<div class="swiper-slide">' +
				'<img src="' + swiperList[k] + '"/></div>';
			$(".swiper-wrapper").append(hot);
			mySwiper.init();
		}
		status = detailObj.status; //招工状态0正在招工 1已经招满
		interviewTime = detailObj.interviewTime; //面试时间
		mechanismId = detailObj.mechanismId; //企业id
		reMoney = detailObj.reMoney; //返费金额
		reTime = detailObj.reTime; //返费时间（单位：月）
		recruitAddress = detailObj.recruitAddress; //面试地址
		var postType = detailObj.postType; //工种0正式1小时
		mechanismName = detailObj.mechanismName; //公司名称

		if(status == 1) { //招满
			$(".stat_zm").show();
			$(".stat_you").hide();
		}
		if(postType == 1) {
			$("#workMoney").html(detailObj.workMoney + "元/小时"); //工价
		} else if(postType == 0) {
			$("#wageRange").html(detailObj.wageRange + "元/月") //综合工资
		}
		$("#post").html(detailObj.postName); //工种名称
		$("#mechanismName").html(detailObj.mechanismName);
		$("#mechanismScore").html(detailObj.mechanismScore); //企业评分
		$("#maxNumber").html(detailObj.maxNumber); //需求人数
		$("#workTypeName").html(detailObj.workTypeName); //岗位名称
		$("#applyNumber").html(detailObj.applyNumber); //已报名数

		$("#mechanismDetails").html(detailObj.mechanismDetails); //公司简介
		$("#mechanismAddress").html(detailObj.mechanismAddress); //公司地址
		$("#workDemand").html(detailObj.workDemand); //入职要求
		$("#workSalary").html(detailObj.workSalary); //薪资福利
		$("#eatSleep").html(detailObj.eatSleep); //住宿餐饮
		$("#workTime").html(detailObj.workTime); //工作性质
		$("#workKnow").html(detailObj.workKnow); //面试材料
		$("#remarks").html(detailObj.remarks); //其他说明

		var key = detailObj.key.split(","); //关键字
		for(var i = 0; i < key.length; i++) {
			var html_key = '<div class="item_b_box">' + key[i] + '</div>';
			$("#res_key").append(html_key);
		}
	});

	var detail_status = sessionStorage.getItem("detail_status");
	//查询是否已报名 、实名认证、咨询联系方式、收藏
	$.ajax({
		type: "get",
		url: serverUrl + "/work/query_isApplyOrIsCollection?sgin=" + userid_sign + "&workId=" + workId,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: 15000,
		success: function(data) {
			var resultObj = eval(data);
			//			console.log("查询是否已报名 、实名认证、咨询联系方式、收藏:" + JSON.stringify(resultObj));
			if(resultObj.code == 0) {
				//咨询联系方式
				teacherTel = resultObj.data.teacher.teacherTel;
				teacherName = resultObj.data.teacher.teacherName;
				$("#serviceName").html(teacherName);
				$("#servicePhone").html(teacherTel);
				var clipboard = new Clipboard('#copy_phone', {
					text: function() {
						return teacherTel;
					}
				});
				clipboard.on('success', function(e) {
					mui.toast("复制成功");
				});
				clipboard.on('error', function(e) {
					mui.toast("复制失败，浏览器不支持");
				});

				var isApply = resultObj.data.isApply; //0已报名1未报名
				userName = resultObj.data.userName; //是否实名
				if(detail_status == 1) { //已登录
					var role = sessionStorage.getItem("role"); //判断角色，禁止报名
					if(role == 4 || role > 6) {
						$("#forbidApply_btn").show();
						$("#stopApply_btn").hide();
						$("#cancelApply_btn").hide();
						$("#entryApply_btn").hide();
					} else {
						if(status == 1) { //停止报名
							$("#stopApply_btn").hide();
							$("#forbidApply_btn").show();
							$("#cancelApply_btn").hide();
							$("#entryApply_btn").hide();
						} else if(isApply == 0) {
							$("#cancelApply_btn").css("display", "block");
							$("#entryApply_btn").css("display", "none");
						} else if(isApply == 1) {
							$("#cancelApply_btn").css("display", "none");
							$("#entryApply_btn").css("display", "block");
						}
					}
				} else {
					if(status == 1) { //停止报名
						$("#stopApply_btn").hide();
						$("#forbidApply_btn").show();
						$("#cancelApply_btn").hide();
						$("#entryApply_btn").hide();
					} else if(isApply == 0) { //已报名
						$("#cancelApply_btn").css("display", "block");
						$("#entryApply_btn").css("display", "none");

					} else if(isApply == 1) { //未报名
						$("#cancelApply_btn").css("display", "none");
						$("#entryApply_btn").css("display", "block");
					}
				}
			} else {
				mui.toast(resultObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});

	//入职报名
	$("#entryApply_btn").click(function() {
		if(!userid_sign) {
			mui.toast("请先登录");
			sessionStorage.setItem("workdetail_sign", 001155); //存储登录后返回详情的标识
			setTimeout(function() {
				window.location.href = "../../login.html?id=" + workId; //去登录并且把当前详情id带过去，以便于返回登录再取
			}, 800);
		} else {
			//判断是否实名
			if(userName != "") {
				//入职报名接口上送
				var param = {};
				param.reTime = reTime;
				param.reMoney = reMoney;
				param.identy = identy;
				param.interviewTime = interviewTime;
				param.mechanismId = mechanismId;
				param.userName = userName;
				param.workId = workId;
				param.recruitAddress = recruitAddress;
				param.mechanismName = mechanismName;
				param.type = 1;
				entryApply(param, function(resultObj) {
					$(".bm_divbox").css("display", "block");
					$(".divbox_m").css("display", "block");
					$("#entryApply_btn").css("display", "none");
					$("#cancelApply_btn").css("display", "block");
					$("#username").html(userName);
					$("#companyname").html(mechanismName);
				});
			} else {
				$(".succ_divbox").css("display", "block");
				$(".succ_box").css("display", "block");
			}
		}
	})

	//咨询窗口显示
	$("#consult_btn").click(function() {
		$(".divbox_yue").css("display", "block");
		$(".bd_divbox").css("display", "block");
	})
	//咨询窗口关闭
	$(".bd_divbox").click(function() {
		$(".bd_divbox").css("display", "none");
		$(".divbox_yue").css("display", "none");
	})
	//立即拨打
	$("#tel_phone").click(function() { //拨打电话
		window.location.href = 'tel://' + teacherTel;
	})

	//报名成功窗口关闭
	$(".bm_divbox").click(function() {
		$(".bm_divbox").css("display", "none");
		$(".divbox_m").css("display", "none");
	})
	//入职报名窗口关闭
	$(".succ_divbox").click(function() {
		$(".succ_divbox").css("display", "none");
		$(".succ_box").css("display", "none");
	})
	//未实名取消入职报名窗口
	$("#cancel_btn").click(function() {
		$(".succ_divbox").css("display", "none");
		$(".succ_box").css("display", "none");
	})
	//未实名确认入职报名窗口
	$("#sure_btn").click(function() {
		var name = $("#name_input").val();
		var flag = checkUsername(name);
		if(flag) {
			//入职报名接口上送
			var param = {};
			param.reTime = reTime;
			param.reMoney = reMoney;
			param.identy = identy;
			param.interviewTime = interviewTime;
			param.mechanismId = mechanismId;
			param.userName = name;
			param.workId = workId;
			param.recruitAddress = recruitAddress;
			param.mechanismName = mechanismName;
			param.type = 1;
			entryApply(param, function(resultObj) {
				$(".bm_divbox").css("display", "block");
				$(".divbox_m").css("display", "block");
				$("#entryApply_btn").css("display", "none");
				$("#cancelApply_btn").css("display", "block");
				$("#username").html(name);
				$("#companyname").html(mechanismName);
			});
		}
	})

	//取消报名
	$("#cancelApply_btn").click(function() {
		$(".esc_divbox").css("display", "block");
		$(".esc_box").css("display", "block");
	})
	//是否取消之前报名窗口关闭
	$(".esc_divbox").click(function() {
		$(".esc_divbox").css("display", "none");
		$(".esc_box").css("display", "none");
	})
	//取消报名-否关闭窗口
	$("#no_btn").click(function() {
		$(".esc_divbox").css("display", "none");
		$(".esc_box").css("display", "none");
	})

	//取消报名-是
	$("#yes_btn").click(function() {
		//取消报名接口上送
		var param = {};
		param.userid_sign = userid_sign;
		param.workId = workId;
		cancelApply(param, function(resultObj) {
			mui.toast("取消成功");
			$(".esc_divbox").css("display", "none");
			$(".esc_box").css("display", "none");
			$("#cancelApply_btn").css("display", "none");
			$("#entryApply_btn").css("display", "block");
		});
	})
})

/**
 * 校验真实姓名
 */
function checkUsername(name) {
	var reg = /^[\u2E80-\u9FFF]+$/; //中文
	if(name == null || name == "") {
		mui.toast("请输入您的姓名");
		return;
	} else if(!reg.test(name)) {
		mui.toast("请输入您的中文姓名");
		return;
	} else if(name.length > 5) {
		mui.toast("姓名最多5位");
		return;
	}
	return true;
}

//查看详情
$("#look_detail_btn").click(function() {
	window.location.href = "interviewreservationlist.html";
})